# -*- coding: utf-8 -*-
import allure
import pytest

from common.utils.allure_report import allure_report
from common.utils.excel_read import excel_read
from common.utils.web_api import web_api

driver_path = "lib/Chrome/chromedriver.exe"
driver = "Chrome"


def pytest_collection_modifyitems(session, config, items):
    for item in items:
        if hasattr(item, 'callspec'):
            use_case = item.callspec.params.get('use_case')
            if use_case:
                if excel_read.col_markers_pos != -1:
                    marker_name = use_case[excel_read.col_markers_pos]
                    if marker_name:
                        mark = getattr(pytest.mark, marker_name)
                        item.add_marker(mark)

                if excel_read.col_severity_pos != -1:
                    marker_severity = use_case[excel_read.col_severity_pos]
                    if marker_severity == "":
                        mark = getattr(pytest.mark, "normal")
                        item.add_marker(mark)
                    else:
                        mark = getattr(pytest.mark, marker_severity)
                        item.add_marker(mark)


@pytest.fixture(autouse=True, name="预置步骤")
def fixture(use_case):
    @allure.step("启动浏览器")
    def open_browser_step():
        with allure.step(f"打开{driver}浏览器"):
            web_api.web_open(driver, driver_path)
    open_browser_step()
    allure_report.execute_preset_condition_steps(use_case, excel_read)
    yield

    @allure.step("停止浏览器")
    def close_browser_step():
        with allure.step(f"强制等待1s"):
            web_api.web_sleep([1000])
        with allure.step(f"关闭{driver}浏览器"):
            web_api.web_quit()
    close_browser_step()
